package com.example.demo.controller;

import com.example.demo.domain.Goods;
import com.example.demo.domain.Order;
import com.example.demo.domain.Picture;
import com.example.demo.domain.User;
import com.example.demo.repository.GoodsMapper;
import com.example.demo.service.GoodsService;
import com.example.demo.service.OrderService;
import com.example.demo.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Create:     Space
 * @Time:       2020-6-16 18:13:00
 * @Describe:
 */
@Controller
public class GoodsController
{
    @Value("${ip}")
    private String ip;
    @Value("${server.port}")
    private String port;
    @Value("${uploadGoodsPath}")
    private String uploadGoodsPath;
    @Value("${uploadIconPath}")
    private String uploadIconPath;
    @Value("${goodsPicPathPattern}")
    private String goodsPicPathPattern;
    @Value("${iconPathPattern}")
    private String iconPathPattern;
    @Resource
    private GoodsService goodsService;
    @Resource
    private OrderService orderService;

    @RequestMapping("/")
    public String index(Model model)
    {
        System.out.println("index");
        model.addAttribute("Act",goodsService.getByRange(0,1));
        model.addAttribute("carousel",goodsService.getByRange(1,3));
        model.addAttribute("display",goodsService.getByRange(4,12));

        model.addAttribute("goodsPicPath", "http://"+ip+":"+port+goodsPicPathPattern);
        return "index";
    }

    @RequestMapping("/goods")
    public String list(Model model,@RequestParam(value = "searchText",required = false) String searchText)
    {
        System.out.println(searchText);
        if(searchText==null)
        {
            model.addAttribute("goods", goodsService.getAll());
            return "admin/goods";
        }
        else if(searchText.length()>0)
        {
            model.addAttribute("goods", goodsService.getSearch(searchText));
            return "admin/goods";

        }
        else
        {
            return "redirect:/goods";
        }
    }


    @GetMapping("/good/{id}")
    public String toEdit(@PathVariable("id") Integer id,Model model)
    {
        System.out.println("get:"+"/good/"+id);
        model.addAttribute("good",goodsService.getGoodsById(id));
        model.addAttribute("goodsPicPath", "http://"+ip+":"+port+goodsPicPathPattern);
        return "admin/addGoods";
    }

    @PutMapping("/good")
    public String updateGoods()
    {
        System.out.println("put:"+"/good");

        return "redirect:/goods";
    }


    @GetMapping("/good")
    public String addGoods(Goods good)
    {
        System.out.println(good);
        System.out.println("get:"+"/good");

        return "admin/addGoods";
    }

    @PostMapping("/good/{id}")
    public String delGoods(@PathVariable("id") Integer id)
    {
        System.out.println("post:"+"/good/"+id);
        goodsService.delGoodsChangeStateById(id);
        return "redirect:/goods";
    }

    @RequestMapping("/search")
    public String loadSerach(Model model,@RequestParam("search") String searchText)
    {
        System.out.println(searchText);
        goodsService.getSearch(searchText);
        model.addAttribute("goods", goodsService.getSearch(searchText));
        model.addAttribute("goodsPicPath", "http://"+ip+":"+port+goodsPicPathPattern);
        return "show/search";
    }


    @RequestMapping("/goodDetail/{id}")
    public String goodDetail(@PathVariable("id") Integer id, Model model)
    {
//      http://localhost:8000/goodDetail/2
        System.out.println(id);
        Goods goods = goodsService.getGoodsById(id);
        model.addAttribute("goods",goods);
        model.addAttribute("goodsPicPath", "http://"+ip+":"+port+goodsPicPathPattern);
        return "show/goodDetail";
    }



}
